using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Utility2DLibrary
{
    public abstract class PrimitivePolyShape
    {
        private List<MassPoint> points = new List<MassPoint>();
        [ContentSerializerIgnore]
        public List<MassPoint> PrimitivePoints
        {
            get { return this.points; }
            set { this.points = value; }
        }

        protected Point centerOfMass=new Point();
        [ContentSerializerIgnore]
        public Point CenterOfMass
        {
            get { return centerOfMass; }
            set { centerOfMass = value; }
        }

        protected Vector2 scale = new Vector2(1,1);
        public Vector2 Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        protected float mass;
        public float Mass
        {
            get { return mass; }
            set { mass = value; }
        }

        //public List<Vector2> VertexList
        //{
        //    get { return this.GetPrimitivePointsAsVectorList(); }
        //    set { List<Vector2> x = value; }
        //}
        private string vertexFile;
        public string VertexFile
        {
            get { return vertexFile; }
            set { vertexFile = value; }
        }

        protected RectangleF boundingBox=new RectangleF();
        [ContentSerializerIgnore]
        public RectangleF LocalBoundingBox
        {
            get{return boundingBox;}
            set{boundingBox=value;}
        }

        public void CalculateCenterOfMass()
        {
            boundingBox = CalculateBoundBox(this.points);
            
            float totalMass = 0;
            Vector2 totalMoment = new Vector2(0, 0);
            foreach(MassPoint mp in this.points)
            {
                totalMass += mp.Mass;
                totalMoment.X += mp.X*mp.Mass;
                totalMoment.Y += mp.Y*mp.Mass;
            }
            centerOfMass.X = totalMoment.X / totalMass;
            centerOfMass.Y = totalMoment.Y / totalMass;
        }

        protected RectangleF CalculateBoundBox(List<MassPoint> pointList)
        {
            RectangleF bounds = new RectangleF(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue);
            for (int i = 0; i < pointList.Count; i++)
            {
                bounds.Left = Math.Min(bounds.Left, pointList[i].X);
                bounds.Right = Math.Max(bounds.Right, pointList[i].X);


                bounds.Top = Math.Max(bounds.Top, pointList[i].Y);
                bounds.Bottom = Math.Min(bounds.Bottom, pointList[i].Y);
            }
            return bounds;
        }

        public List<Vector2> GetPrimitivePointsAsVectorList()
        {
            List<Vector2> vectorList = new List<Vector2>(points.Count);
            foreach (MassPoint massPoint in points)
            {
                vectorList.Add(massPoint.ToVector2());
            }
            return vectorList;
        }
    }
}
